package com.topvision.elementanim;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private View mView;
    private MyView myView;
    private ImageView mRelativeLayout;
    private ImageView mRelativeLayout2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRelativeLayout = (ImageView) findViewById(R.id.activity_main);
        mRelativeLayout2 = (ImageView) findViewById(R.id.activity_main2);

        mView = (View) findViewById(R.id.view);
        myView = (MyView) findViewById(R.id.my_view);
        mView.setTransitionName("name1");
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, mView, "name1").toBundle();
                ActivityCompat.startActivity(MainActivity.this,new Intent(MainActivity.this,ViewerActivity.class),bundle);
            }
        });

        Bitmap bitmap = FastBlur.doBlur(BitmapFactory.decodeResource(getResources(), R.drawable.test_image), 100, false);
        getColor(bitmap);
        mRelativeLayout.setImageBitmap(bitmap);

    }

    public void start(View view) {
        myView.startAnim();
    }

    private int color;
    private void getColor(Bitmap bitmap) {
        int pos, row, col, clr;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixSrc = new int[width * height];
        int[] pixNvt = new int[width * height];
        bitmap.getPixels(pixSrc, 0, width, 0, 0, width, height);
        for (row = 0; row < height; row++) {
            for (col = 0; col < width; col++) {
                pos = row * width + col;
                pixSrc[pos] = (Color.red(pixSrc[pos])
                        + Color.green(pixSrc[pos]) + Color.blue(pixSrc[pos])) / 3;
                color += (Color.red(pixSrc[pos])
                        + Color.green(pixSrc[pos]) + Color.blue(pixSrc[pos]));
            }
        }
        int i = color / height / width;
        System.out.println(i);
    }
}
